﻿using Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using 纺织贸易管理系统;

namespace BLL
{
    public static class 采购入库单BLL
    {
        public static void 保存单据(DanjuTable danju, List<danjumingxitable> danjumingxitables)
        {
            if (SysInfo.GetInfo.own != "审核制")
            {
                danju.zhuangtai = "已审核";
            }
            else
            {
                danju.zhuangtai = "未审核";
            }
            danju.dh = 单据BLL.检查单号(danju.dh);
            DanjuTableService.InsertDanjuTable(danju);
            foreach (var m in danjumingxitables.Where(x => !string.IsNullOrEmpty(x.pingming)).ToList())
            {
                m.danhao = danju.dh;
                m.houzhengli = danju.jiagongleixing;
                danjumingxitableService.Insertdanjumingxitable(m);
            }
            if (SysInfo.GetInfo.own != string.Empty)
            {
                if (SysInfo.GetInfo.own != "审核制")
                {
                    单据审核(danju.dh);
                }
            }
            else
            {
                单据审核(danju.dh);
            }
        }
        public static void 修改单据(DanjuTable danju, List<danjumingxitable> danjumingxitables)
        {
            danjumingxitables = danjumingxitables.Where(x => !string.IsNullOrEmpty(x.pingming)).ToList();
            if (SysInfo.GetInfo.own != string.Empty)
            {
                if (SysInfo.GetInfo.own != "审核制")
                {
                    单据反审核(danju.dh);
                }
            }
            else
            {
                单据反审核(danju.dh);
            }
            Connect.dbcontext.Updateable(danju).ExecuteCommand();
           Connect.dbcontext.Deleteable <danjumingxitable>().Where (x => x.danhao == danju.dh).ExecuteCommand ();
            foreach (var m in danjumingxitables)
            {
                m.danhao = danju.dh;
                m.houzhengli = danju.jiagongleixing;
               
            }
            Connect.dbcontext .Insertable(danjumingxitables ).ExecuteCommand();
            if (SysInfo.GetInfo.own != string.Empty)
            {
                if (SysInfo.GetInfo.own != "审核制")
                {
                    单据审核(danju.dh);
                }
            }
           
        }
        public static void 删除单据(string danhao)
        {
          
            if (SysInfo.GetInfo.own != string.Empty)
            {
                if (SysInfo.GetInfo.own != "审核制")
                {
                    单据反审核(danhao);
                }
            }
            Connect.dbcontext.Deleteable<DanjuTable>().Where(x => x.dh == danhao).ExecuteCommand();
            Connect.dbcontext.Deleteable <danjumingxitable >().Where (x => x.danhao == danhao).ExecuteCommand ();         
        }
        public static void 单据审核(string danhao)
        {
            var danju = DanjuTableService.GetOneDanjuTable(x => x.dh == danhao);
            var danjumingxitables = Connect.dbcontext.Queryable<danjumingxitable>().Where(x => x.danhao == danhao).ToList();
            danju.zhuangtai = "已审核";
            来往明细BLL.增加来往记录(danju);
            财务BLL.增加应付款(danju);
            财务BLL.增加应收发票(danju);         
            库存BLL.增加库存(danjumingxitables, danju);
            Connect.dbcontext.Updateable(danju).ExecuteCommand();         
        }
        public static void 单据反审核(string danhao)
        {
            var danju = DanjuTableService.GetOneDanjuTable(x => x.dh == danhao);
            var danjumingxitables = danjumingxitableService.Getdanjumingxitablelst(x => x.danhao == danhao); 
            来往明细BLL.删除来往记录(danju);
            财务BLL.减少应付款(danju);
            财务BLL.减少应收发票(danju);
            库存BLL.减少库存(danjumingxitables, danju);
            danju.zhuangtai = "未审核";
           Connect.dbcontext.Updateable(danju).ExecuteCommand ();

        }
    }
}
